//
//  AttentionViewController.m
//  斗鸡TV
//
//  Created by admin on 16/3/10.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "AttentionViewController.h"
#import "ZongYiModel.h"
#import "ZongYiViewCell.h"
#import "AVPlayerViewController.h"
#import "UIScrollView+SVPullToRefresh.h"

//#define Atten_Url @"http://cbox.cntv.cn/json2015/fenleierjiye/zongyi/yangshilanmugengduo/index.json"
#define Atten_Url @"http://cbox.cntv.cn/json2015/fenleierjiye/zongyi/zongyiqiangdangdengduo/index.json"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface AttentionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    ZongYiModel * zm;
    UICollectionView * collectionV;
}
@end

@implementation AttentionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"综艺";
    self.view.backgroundColor = [UIColor whiteColor];
    UICollectionViewFlowLayout * layout = [UICollectionViewFlowLayout new];
    layout.itemSize = CGSizeMake(WIDTH/2-2, 130);
    layout.minimumInteritemSpacing = 1;

    collectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT-64-48) collectionViewLayout:layout];
    collectionV.delegate = self;
    collectionV.dataSource = self;
    collectionV.backgroundColor = [UIColor clearColor];
    [collectionV registerClass:[ZongYiViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:collectionV];
    [self loadHttpData];
    
    __weak AttentionViewController * weakSelf = self;
    [collectionV addPullToRefreshWithActionHandler:^{
        
        [weakSelf loadHttpData];
    }];
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return zm.itemList.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZongYiViewCell * cell = [collectionV dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    [cell setCellData:zm.itemList[indexPath.row]];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SubZongYiModel * sm = zm.itemList[indexPath.row];
    AVPlayerViewController * av = [AVPlayerViewController new];
    av.sm = sm;
    [self.navigationController pushViewController:av animated:YES];
}

-(void)loadHttpData
{
    [self showLoading];
    [self httpGetUrl:Atten_Url params:nil successBlock:^(NSDictionary *dict) {
        NSLog(@"%@",dict);
        if (dict[@"data"])
        {
            zm = [[ZongYiModel new]initWithJsonDic:dict[@"data"]];
            
        }
        
        [collectionV reloadData];
        [self dismissLoading];
        [collectionV.pullToRefreshView stopAnimating];
    } errorBlock:^(NSError *error) {
         [collectionV.pullToRefreshView stopAnimating];
        
    }];
}

-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden=NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
